﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerInventoryItemCategoryDao : IInventoryItemCategoryDao
    {
        public List<InventoryItemCategory> GetInventoryItemCategories()
        {
            string procedures = "Get_All_InventoryItemCategories";

            return Db.ReadList(procedures, true, Make);
        }

        public InventoryItemCategory GetInventoryItemCategoryByBranch(string restaurantId, string inventoryCategoryId)
        {
            string procedures = "Get_All_InventoryItemCategory_ByBranch";

            object[] parms = { "@RestaurantID", restaurantId,
                                "@InventoryCategoryId", inventoryCategoryId  };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<InventoryItemCategory> GetInventoryItemCategoriesByBranch(string restaurantId, bool isMaterial)
        {
            string procedures = "Get_All_InventoryItemCategories_ByBranch";

            object[] parms = { "@RestaurantID", restaurantId,
                               "@IsMaterial", isMaterial };
            return Db.ReadList(procedures, true, Make, parms); ;
        }

        public InventoryItemCategory GetInventoryItemCategory(string inventoryCategoryId)
        {
            string procedures = "Get_All_InventoryItemCategory_ById";

            object[] parms = { "@InventoryCategoryID", inventoryCategoryId };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<InventoryItemCategory> GetInventoryItemCategoriesByBranchAndIsActive(string restaurantId, bool isMaterial, bool isActive)
        {
            string procedures = "Get_All_InventoryItemCategories_ByBranch_AndIsActive";

            object[] parms = {  "@RestaurantId", restaurantId,
                                "@IsActive", isActive,
                                "@IsMaterial", isMaterial};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetCountAcountingObjectCategoryByParentId(string parentId)
        {
            string procedures = "Proc_GetCount_InventoryItemCategory_ByParentId";

            object[] parms = { "@ParentId", parentId };

            return Db.Count(procedures, true, parms);
        }

        public int GetCountInventoryItemCategoryByParentId(string parentId)
        {
            string procedures = "Get_InventoryItemCategory_ByParentID";

            object[] parms = { "ParentID", parentId };

            var inventoryItemCategories = Db.ReadList(procedures, true, Make, parms);
            if (inventoryItemCategories != null) { return inventoryItemCategories.Count; }
            else { return 0; }
        }

        public void InsertInventoryItemCategory(InventoryItemCategory inventoryItemCategory)
        {
            string sql = "Insert_InventoryItemCategory";

            inventoryItemCategory.InventoryCategoryID = Db.Insert(sql, true, Take(inventoryItemCategory));
        }

        public void UpdateInventoryItemCategory(InventoryItemCategory inventoryItemCategory)
        {
            string sql = "Update_InventoryItemCategory";

            Db.Update(sql, true, Take(inventoryItemCategory));
        }

        public void DeleteInventoryItemCategory(InventoryItemCategory inventoryItemCategory)
        {
            string sql = "Delete_InventoryItemCategory";

            Db.Update(sql, true, Take(inventoryItemCategory.InventoryCategoryID));
        }

        private static Func<IDataReader, InventoryItemCategory> Make = reader =>
           new InventoryItemCategory
           {
               InventoryCategoryID = reader["InventoryCategoryID"].AsString(),
               ParentID = reader["ParentID"].AsString(),
               IsParent = reader["IsParent"].AsBool(),
               InventoryCategoryCode = reader["InventoryCategoryCode"].AsString(),
               InventoryCategoryName = reader["InventoryCategoryName"].AsString(),
               IsMaterial = reader["IsMaterial"].AsBool(),
               RestaurantID = reader["RestaurantID"].AsString(),
               IsActive = reader["IsActive"].AsBool(),
               Description = reader["Description"].AsString()
           };

        private object[] Take(InventoryItemCategory inventoryItemCategory)
        {
            return new object[]  
            {
                "@InventoryCategoryID", inventoryItemCategory.InventoryCategoryID,
                "@InventoryCategoryCode", inventoryItemCategory.InventoryCategoryCode,
                "@InventoryCategoryName", inventoryItemCategory.InventoryCategoryName,
                "@ParentID", inventoryItemCategory.ParentID,
                "@IsParent", inventoryItemCategory.IsParent,
                "@IsMaterial", inventoryItemCategory.IsMaterial,
			    "@RestaurantID", inventoryItemCategory.RestaurantID,
                "@Description", inventoryItemCategory.Description ,
                "@IsActive", inventoryItemCategory.IsActive
            };
        }

        private object[] Take(string inventoryCategoryID)
        {
            return new object[]  
            {
                "@InventoryCategoryID", inventoryCategoryID,
            };
        }
    }
}
